package com.util.connection;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;



public class ConnectionManager {
    

	 static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
	 //static final String DB_URL_GABOR = "jdbc:mysql://localhost/gb_final";
	// static final String DB_URL_GABOR = "jdbc:mysql://localhost/gb_newfeture_p1";
	// static final String DB_URL_CURVELET = "jdbc:mysql://localhost/cur_final";
	// static final String DB_URL_CURVELET = "jdbc:mysql://localhost/curblet_newfeture_p3_1";
	 
	static final String DB_URL_GABOR = "jdbc:mysql://172.16.10.28/gb_scaledb_web_p1";
	static final String DB_URL_CURVELET = "jdbc:mysql://172.16.10.28/cur_scaledb_web_p3";
	 static final String USER = "root";
	 static final String PASS = "";
	   
    private  static DataSource ds = null;
    
     
    //Marking default constructor private
    //to avoid direct instantiation.
    private ConnectionManager() {
    }
 
    public static Connection getConnectionPool() {
     
            Connection conn=null;       	
        	try {
	    			if (ds==null) 
	    			{
	    				Context initContext = new InitialContext();
	    				Context envContext = (Context) initContext.lookup("java:/comp/env");
	    				ds = (DataSource) envContext.lookup("jdbc/mysqlconn");
	    			}
	    			conn = ds.getConnection();
    			} 
        	catch (Exception e) 
        		{
    		      try {
    		          Thread.currentThread();
    		          Thread.sleep(2000);
    		          conn = ds.getConnection();
    		        }
    		        catch (Exception ex) {
    					System.out.println("******##############################################################################");
    					System.out.println(ex.getMessage());
    					System.out.println("******############################ Exception in Get Connection ################################");
    		     }
    			System.out.println("##############################################################################");
    			System.out.println(e.getMessage());
    			System.out.println("##############################################################################");
    		}
    		
        
     
        return conn;
    }
    
    //Get instance for class SimpleSingleton
    public static Connection getConnectionGabor() {
     
            Connection conn=null;       	
        	try {
        		 Class.forName("com.mysql.jdbc.Driver");
        	     System.out.println("Connecting to a selected database...");
        	     conn = DriverManager.getConnection(DB_URL_GABOR, USER, PASS);
    			} 
        	catch (Exception e) 
        		{
    		      try {
    		          Thread.currentThread();
    		          Thread.sleep(2000);
    		          conn = ds.getConnection();
    		        }
    		        catch (Exception ex) {
    					System.out.println("******##############################################################################");
    					System.out.println(ex.getMessage());
    					System.out.println("******############################ Exception in Get Connection ################################");
    		     }
    			System.out.println("##############################################################################");
    			System.out.println(e.getMessage());
    			System.out.println("##############################################################################");
    		}
    		
     
        return conn;
    }
    
    public static Connection getConnectionCurvelet() {
        
        Connection conn=null;       	
    	try {
    		 Class.forName("com.mysql.jdbc.Driver");
    	     System.out.println("Connecting to a selected database...");
    	     conn = DriverManager.getConnection(DB_URL_CURVELET, USER, PASS);
			} 
    	catch (Exception e) 
    		{
		      try {
		          Thread.currentThread();
		          Thread.sleep(2000);
		          conn = ds.getConnection();
		        }
		        catch (Exception ex) {
					System.out.println("******##############################################################################");
					System.out.println(ex.getMessage());
					System.out.println("******############################ Exception in Get Connection ################################");
		     }
			System.out.println("##############################################################################");
			System.out.println(e.getMessage());
			System.out.println("##############################################################################");
		}
		
 
    return conn;
}
    
    public static void closeConnectionPool(Connection connection) {
		try {
			connection.close();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			connection = null;
		}
	}
    
    
}


